package com.baba.client.service.impl;

import com.baba.client.dao.ClientUserDao;
import com.baba.client.entity.ClientHistory;
import com.baba.client.entity.ClientUser;
import com.baba.client.service.ClientHistoryService;
import com.baba.client.service.ClientUserService;
import com.baba.client.utils.IpUtil;
import com.baba.client.utils.MD5Utils;
import com.baba.client.utils.redis.JwtUtil;
import com.baba.client.utils.retruns.ResponseResult;
import com.baba.client.utils.retruns.ResultCode;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.convert.EntityWriter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by cengyujun on 2020/6/9 11:53 上午
 */

@Slf4j
@Service
@Transactional
public class ClientUserServiceImpl extends ServiceImpl<ClientUserDao,ClientUser> implements ClientUserService {
    @Autowired
    private ClientUserService clientUserService;
    @Autowired
    private ClientHistoryService clientHistoryService;
    @Resource
    private ClientUserDao clientUserDao;

    /**
     * 用户登入
     * @param userName
     * @param userPassword
     * @param request
     * @return
     * @throws IOException
     */
    public ResponseResult login(String userName ,String userPassword, HttpServletRequest request) throws IOException {
            try {
                ClientUser clientUser=null;
                // 加密
                String userPassword2=MD5Utils.MD5Encode(userPassword);
                clientUser=clientUserService.getOne(Wrappers.<ClientUser>lambdaQuery()
                        .eq(ClientUser::getUserName,userName)
                        .eq(ClientUser::getUserPassword,userPassword2));

                 if (clientUser!=null){
                     ClientHistory clientHistory = new ClientHistory();
                     clientHistory.setLoginDep(clientUser.getUserDep());
                     clientHistory.setLoginIp(IpUtil.getIpAddr(request));
                     clientHistory.setLoginType(IpUtil.getDeviceType(request));
                     clientHistory.setUserName(userName);
            if (!clientHistoryService.save(clientHistory)){
                return  ResponseResult.failure(ResultCode.PASSWORD_ERR);
            }

                String token= JwtUtil.sign(userName);
                Map<String,Object> data=new HashMap<>();
                data.put("clientUser",clientUser);
                data.put("token",token);
                return ResponseResult.success(data);
                 }
                return  ResponseResult.failure(ResultCode.PASSWORD_ERR);
            }catch (Exception e){
                e.printStackTrace();
            }
    return  ResponseResult.failure(ResultCode.LOGIN_DATE);
    }

    /**
     * 添加用户
     * @param clientUser
     * @return
     */
   public ResponseResult addUser (ClientUser clientUser){
        try {
            String password =MD5Utils.MD5Encode(clientUser.getUserPassword());
            clientUser.setUserPassword(password);
            if (clientUserService.save(clientUser));

            return ResponseResult.success();



        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseResult.failure("添加用户失败");
   }

    /**
     * 查询所以用户
     * @return
     */
    public  ResponseResult queryAll(){
        try {
             Map<String ,Object> data = new HashMap();
            List<ClientUser> list =clientUserService.list(null);
            data.put("list",list);
              return ResponseResult.success(data);
        } catch (Exception e){
            e.printStackTrace();
        }
        return ResponseResult.failure("查询失败");
    }

    /**
     * 更新系统用户信息
     * @param clientUser
     * @return
     */
    public ResponseResult updateUser(ClientUser clientUser){
        try {
            if (clientUserService.updateById(clientUser)){
                return ResponseResult.success();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseResult.failure("更新失败");
    }

    /**
     *
     * @param clientUser
     * @return
     */
    public ResponseResult deletUser(ClientUser clientUser){
        try {
            if (clientUserService.remove(Wrappers.<ClientUser>lambdaQuery().eq(ClientUser::getUserId,clientUser.getUserId())))
                return ResponseResult.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseResult.failure("删除失败");
    }
//    @Autowired
//    ClientUserDao clientUserDao;


//    @Override
//    public ResponseResult login(String userNane, String userPasword) {
//        try{
//            Map<String, Object> data = new HashMap<String, Object>();
//            data.put("userNane",userNane);
//            data.put("userPasword",userPasword);
//            List< ClientUser> clientUser=clientUserDao.selectByMap(data);
//            return  null;
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//
//        return  null;
//    }

//    @Override
//    public ClientUser queryById(Integer userId) {
//        return null;
//    }
//
//    @Override
//    public List<ClientUser> queryAllByLimit(int offset, int limit) {
//        return null;
//    }
//
//    @Override
//    public ClientUser insert(ClientUser clientUser) {
//        return null;
//    }
//
//    @Override
//    public ClientUser update(ClientUser clientUser) {
//        return null;
//    }
//
//    @Override
//    public boolean deleteById(Integer userId) {
//        return false;
//    }
}
